//
//  WLShareView.swift
//  WLVehicleConfiguration
//
//  Created by dowZhang on 2017/9/22.
//  Copyright © 2017年 Yongpeng.Zhu. All rights reserved.
//

private let cellWidth: CGFloat = (UIScreen.main.bounds.size.width / 4.0)
private var selectViewHeight: CGFloat = 200//260
private let shareViewCellIdentifier = "WLShareViewCell"


protocol WLShareViewDelegate {
    func selectedItem(item:WLSharePlatform)
}

import UIKit
//import FDBusinessService

enum WLSharePlatform: Int {
    case NIO = 0
    case weChate
    case friends
    case weiBo
    case save
    case qrCode
}


class WLShareView: UIView {
    
    var delegate:WLShareViewDelegate?
    
    //MARK: lazy
    lazy var selectView: UIView = {
        let selectView = UIView()
        selectView.backgroundColor = .white
        return selectView
    }()
    
    //分享平台
    var sharePlatformArray: [[String : String]]?
    
    lazy var shareInformationCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: cellWidth, height: 104.5)
        let collectionView = UICollectionView(frame: CGRect.init(x: 0, y: 65, width: UIScreen.main.bounds.size.width, height: 135), collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(UINib.init(nibName: "WLShareViewCell", bundle: nil), forCellWithReuseIdentifier: shareViewCellIdentifier)
        collectionView.delegate = self
        collectionView.dataSource = self
        
        return collectionView
    }()
    
    var isShowQRCode: Bool = true //true 为配置页分享,false为了解ES8页面分享
    
    convenience init(_ isShowQRCode: Bool) {
        self.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        self.isShowQRCode = isShowQRCode
        if isShowQRCode{
//            if let path = vom_bundle?.path(forResource: "WLShareInformationNoWeibo.plist", ofType: nil) {
//                sharePlatformArray = WLShareInformationNoWeibo
//            }
        }else{
//            if let path = vom_bundle?.path(forResource: "WLShareInformation.plist", ofType: nil) {
//                sharePlatformArray = WLShareInformation
//                    NSArray.init(contentsOfFile: path) as? [[String : String]]
//            }
        }
        
        setupUI()
    }
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.4)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: private methods
    private func setupUI() {
        
        //上部view
        let topView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - selectViewHeight))
        self.addSubview(topView)
        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(backgroundViewTaped))
        topView.addGestureRecognizer(tapGesture)
        
        //底部view
        selectView.frame = CGRect.init(x: 0, y: UIScreen.main.bounds.size.height + 55, width: UIScreen.main.bounds.size.width, height: selectViewHeight)
        self.addSubview(selectView)
        
        //分享到
        let shareLabel = UILabel.init(frame: CGRect.init(x: 0, y: 13, width: UIScreen.main.bounds.size.width, height: 25))
        shareLabel.text = "分享到"
        shareLabel.textAlignment = .center
        shareLabel.textColor = UIColor.init(red: 51, green: 51, blue: 51)
        if let font = UIFont(name: "PingFangSC-Regular", size: 17) {
            shareLabel.font = font
        } else {
            shareLabel.font = UIFont.systemFont(ofSize: 17)
        }
        
        let lineLabel = UILabel.init(frame: CGRect.init(x: 0, y: 50, width: UIScreen.main.bounds.size.width, height: 0.5))
        lineLabel.backgroundColor = UIColor("#E5E5E5")
        
        let closeButton = UIButton.init(type: .custom)
        closeButton.frame = CGRect.init(x: UIScreen.main.bounds.size.width - 35, y: 18, width: 15, height: 15)
        closeButton.setBackgroundImage(UIImage.init(named: "closeEva", in: nil, compatibleWith: nil), for: .normal)
        closeButton.addTarget(self, action: #selector(backgroundViewTaped), for: .touchUpInside)
        
        selectView.addSubview(lineLabel)
        
        selectView.addSubview(shareLabel)
        
        selectView.addSubview(shareInformationCollectionView)
        
        selectView.addSubview(closeButton)
        
        showSelectView()
    }
    
    
    private func showSelectView() {
        
        UIView.animate(withDuration: 0.4) {
            self.selectView.frame.origin.y = UIScreen.main.bounds.size.height - selectViewHeight
        }
        
        var keyWindow: UIWindow?
        let windows = UIApplication.shared.windows
        for window in windows {
            if window.isMember(of: UIWindow.self) && (window.bounds.equalTo(UIScreen.main.bounds)) && !window.isHidden {
                keyWindow = window
                break
            }
        }
        
        if keyWindow == nil {
            keyWindow = UIApplication.shared.keyWindow
        }
        
        keyWindow?.addSubview(self)
        
    }
    
    
    fileprivate func hideSelectView() {
        
        UIView.animate(withDuration: 0.2, animations: {
            self.selectView.frame.origin.y = UIScreen.main.bounds.size.height + 55
        }) { ( _ ) in
            self.removeFromSuperview()
        }
    }
    //MARK: event response
    //点击上部隐藏控件
    @objc private func backgroundViewTaped() {
//        eventReport(FDEventReport(event: MyConfigurator_Share_Cancel_Click, message: [:]))
        self.hideSelectView()
    }
    
}

extension WLShareView: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let count = sharePlatformArray?.count {
            return count
        } else {
            return 0
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: shareViewCellIdentifier, for: indexPath) as! WLShareViewCell
        if let imageName = sharePlatformArray?[indexPath.row]["imageName"] {
            cell.shareLogImageView.image = UIImage.init(named: imageName, in: nil, compatibleWith: nil)
        }
        
        cell.shareTitleLabel.text = sharePlatformArray?[indexPath.row]["title"]
//        if indexPath.row > 3 || !isShowQRCode {
//            cell.lineView.isHidden = true
//        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var selectedPlatform: WLSharePlatform?
        switch indexPath.row {
        case 0:
            selectedPlatform = WLSharePlatform.NIO
        case 1:
            selectedPlatform = WLSharePlatform.weChate
        case 2:
            selectedPlatform = WLSharePlatform.friends
        case 3:
            if isShowQRCode{
                selectedPlatform = WLSharePlatform.qrCode
            }else{
                selectedPlatform = WLSharePlatform.weiBo
            }
            
        default:
            break
        }
        
        if let d = self.delegate,let platform = selectedPlatform{
            d.selectedItem(item: platform)
            hideSelectView()
        }
    }
}

